
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**miniTiiger
 *
 * @author Sirli, Raido, Argo
 */
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
import java.io.*;

public class TheEnd extends Canvas implements CommandListener{
    Joonistus midlet;
    static int old;
    int koord_X = getWidth()/2;
    int koord_Y = getHeight()/2;
    int kinkidearv_konstant = 5, kingivahe_konstant = 40, mitu = 5, kiirus = 8;
    int[][] kingiandmed = new int[5][3];
    int[] kink = {0, 0, 0, 0, 0};
    int kingivahe = 5;
    int elud = 3, punktid = 0, kiiruse_lisaja = 0, kingi_lisaja = 0, kinke_lisatud = 0;
    int ajakulg = 0, lahenemine_majale = 40, plahvatus = 100, tiiger_tuleb = 120;
    int tiiger_piilub_X = getWidth(), tiiger_piilub_Y = getHeight();
    private Command cmExit;  // Exit midlet
    private Command reStart;  // Exit midlet

     static byte[] intBaitideks(int a) throws IOException{
     ByteArrayOutputStream bos=new ByteArrayOutputStream();
     DataOutputStream dos=new DataOutputStream(bos);
     dos.writeInt(a);
     dos.close();
     return bos.toByteArray();
  }
    static int baididIntiks(byte[] b) throws IOException {
    ByteArrayInputStream bis=new ByteArrayInputStream(b);
    DataInputStream dis=new DataInputStream(bis);
    return dis.readInt();
  }

    public TheEnd(Joonistus midlet){
        this.midlet = midlet;
        cmExit = new Command("Välju", Command.EXIT, 1);
        reStart = new Command("Uuesti", Command.ITEM, 1);
        addCommand(cmExit);
        addCommand(reStart);
        setCommandListener(this);
    }

    static RecordStore rs;
    static String hoidlanimi = "hoidla";
    
    public static void save(){
       try{
           rs=RecordStore.openRecordStore(hoidlanimi, true);
                if(rs.getNumRecords()==0){
                    byte[] bm=intBaitideks(Louend.punktid);
                    rs.addRecord(bm, 0, bm.length);
                    old=0;
                } else {
                    old=baididIntiks(rs.getRecord(1));
                    if(old<Louend.punktid){
                        byte[] bm=intBaitideks(Louend.punktid);
                        rs.setRecord(1, bm, 0, bm.length);
                    }
                }
           rs.closeRecordStore();
       }catch(Exception e){e.printStackTrace();}
    }

    protected void paint(Graphics g){
        save();
        g.setColor(250, 0, 0);
        g.setStrokeStyle(Graphics.DOTTED);
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        g.drawString("Püüdsid kinni", 10, 10, Graphics.TOP | Graphics.LEFT);
        if(Louend.punktid==1){
            g.drawString(" "+Louend.punktid+" paki!", 10, 30, Graphics.TOP | Graphics.LEFT);
        }else{
            g.drawString(" "+Louend.punktid+" pakki!", 10, 30, Graphics.TOP | Graphics.LEFT);
        }
        if(Louend.punktid > old){
            g.drawString("Parim tulemus!", 10, 50, Graphics.TOP | Graphics.LEFT);
        }
    }

    protected void keyPressed(int kood){
        if(getGameAction(kood)==Canvas.FIRE){
           midlet.display.setCurrent(midlet.intro);
           Louend.elud = 5;
           Louend.punktid = 0;
         }
    }

    public void commandAction(Command c, Displayable d){
        if (c == cmExit){
            midlet.exitMIDlet();
        }
        if (c == reStart){
            midlet.display.setCurrent(midlet.louend);
            Louend.elud = 5;
            Louend.punktid = 0;

        }
    }
}
